<?php
require_once('core/pglob.php');
require_once('quanliadmin/app/comm/comm_page.php');
require_once('quanliadmin/app/project/xajax/ajax_error.php');
require_once('quanliadmin/app/comm/comm_login.php');
require_once('projman/onlinehandler.php');
/**
 * 这是一个错误日志的类包括日志的询
 *
 */
class pa_errorLog{
	public function execute(){
		global $G_PGLOB;
		if(isset($_REQUEST['operFlg'])){
			$operFlg = $_REQUEST['operFlg'];
		}else{
			$operFlg = 1;
		}
		switch ($operFlg){
			case 1:
				$this->init($operFlg);
				break;
			case 2:
				$this->search_error($operFlg);
				break;
			case 3:
				$this->errorExport();
				break;
			case 4:
				$this->errorImport();
				break;
			default:
				$G_PGLOB->smarty->assign('error_msg','');
				$G_PGLOB->smarty->display('comm/errorDisplay.tpl');	
			break;
		}
	}
	/**
	 * 页面初始化
	 *
	 * @param unknown_type $operFlg
	 */
	public function init($operFlg){
		global $G_PGLOB;
		if(!is_login())
			userLogin('/quanliadmin/app/project/errorLog.pscript?operFlg=1');
		
		$G_PGLOB->smarty->assign('operFlg',$operFlg);
		$G_PGLOB->smarty->display('project/errorLog.tpl');
	}
	/**
	 * 错误日志查找
	 *
	 * @param unknown_type $operFlg
	 * @param unknown_type $
	 */
	public function search_error($operFlg){
		global $G_PGLOB;
		$username = is_login();
		$sqlstr = 'where';
		if(empty($username)){
			userLogin('/quanliadmin/app/project/errorLog.pscript?operFlg=1');
		}else{
			$sqlstr .= " and chargeAuthor='".$username."'";
		}
			
		
		
		$handleResult	= isset($_REQUEST['handleResult']) ? intval($_REQUEST['handleResult']) : null;
		$startDate		= isset($_REQUEST['startDate']) ? trim($_REQUEST['startDate']) : null;
		$endDate		= isset($_REQUEST['endDate']) ? trim($_REQUEST['endDate']) : null;
		$pageIndex		= isset($_REQUEST['pageIndex'])?$_REQUEST['pageIndex']:1;
		
		
		
		
		

		
		
		
		if(!empty($handleResult) || $handleResult == 0)
			$sqlstr .= ' and handleResult='.$handleResult;
		
		if(!empty($startDate) && !empty($endDate)){
			$startTime	= $startDate.' 00:00:00';
			$endTime	= $endDate.' 23:59:59';
			$sqlstr .= sprintf(" and timeStamp between '%s' and '%s'",$startTime,$endTime);
		}
			
		if($sqlstr!='where'){
			$sqlstr = str_replace('where and','where',$sqlstr);
		}else{
			$sqlstr = '';
		}
		$perpage = 20;
		$sqlLimit = sprintf('LIMIT %d,%d', ($pageIndex-1) * $perpage, $perpage);
		
		
		
		try{
			
			$count  = $G_PGLOB->DBCA->inc_load('error','count.error.query.inc',$sqlstr);
			if($count->count == 0)
				throw new Exception('查询到0条日志记录', -1000);
			
			
			$error_info = &$G_PGLOB->DBCA->inc_query('error','error.query.inc',array($sqlstr,$sqlLimit));
			
			$footHtml = getFooterHtml($count->count, $pageIndex, $perpage);
			$G_PGLOB->smarty->assign_by_ref('error_info',$error_info);
			$G_PGLOB->smarty->assign('listCount',$count->count);
			$G_PGLOB->smarty->assign('startDate',$startDate);
			$G_PGLOB->smarty->assign('endDate',$endDate);
			$G_PGLOB->smarty->assign('handleResult',$handleResult);
			$G_PGLOB->smarty->assign('footHtml', $footHtml);
			$G_PGLOB->smarty->display('project/errorLog.tpl');	
		}catch(Exception $e){
			if($e->getCode() == -1000){
				$G_PGLOB->smarty->assign('error_msg',$e->getMessage());	
				$G_PGLOB->smarty->display('project/errorLog.tpl');
			}else{
				error_log($e->getMessage());
				$G_PGLOB->smarty->display('comm/errorDisplay.tpl');
			}
		}
		
		
	}
	
	/**
	 * 错误日志信息导出
	 * 
	 */
	public function errorExport(){
		header("Content-type: application/vnd.ms-csv; charset=utf-8");
		header("Content-Disposition: attachment; filename=".iconv('UTF-8', 'GBK','php_error_log'.date("YmdHis")).".csv");
		global $G_PGLOB;
		$username = is_login();
		if(empty($username)){
			userLogin('/quanliadmin/app/project/errorLog.pscript?operFlg=1');
		}
		$sql = 'select errorLevel,occurTime,errorMessage,errorFile,errorLine,handleResult,domainName,appServer from php_error_log where chargeAuthor = ?';
		$error_info = &$G_PGLOB->DBCA->stmt_query($sql,'s',$username);
		$header = '错误等级,错误时间,错误信息,错误文件,错误行,错误结果,所属域名,服务器ip'."\n";
		$content = '';
		for($i=0;$i<count($error_info);$i++){
			$content .= $error_info[$i]->errorLevel.',';
			$content .= $error_info[$i]->occurTime.',';
			$errorMessage = str_replace(',','，',$error_info[$i]->errorMessage);
			$content .= $errorMessage.',';
			$content .= $error_info[$i]->errorFile.',';
			$content .= $error_info[$i]->errorLine.',';
			$content .= $error_info[$i]->handleResult.',';
			$content .= $error_info[$i]->domainName.',';
			$content .= $error_info[$i]->appServer."\n";
		}
		$all = $header .$content;
		echo iconv('UTF-8', 'GBK',$all);
	}
	
	/**
	 * 错误信息导入
	 * 
	 */
	public function errorImport(){
		global $G_PGLOB;
	    set_time_limit(0); 
		$serverInfo = array(
		    'ballnet' => array(
		        '10.129.209.187' => '187_error.log',
		        '10.129.209.186' => '186_error.log',
		    ),
		    'sports' =>array(
		    	'10.129.209.194' => 'php_error.log'
		    )
		);
		$serverDomain = array(
		    'ballnet' => 'www.ballnet.cn',
		    'sports' => 'shop.sports.com'
		);
		define('PHP_ERROR_LOG_DIR', '/var/inonesoft/log/product_logs/');
		
		//循环遍历出错误日志
		
		foreach ($serverInfo as $key => $value) {
			foreach ($value as $k => $v) {
				//判断文件是否存在
				if (file_exists(PHP_ERROR_LOG_DIR . $v)) {
				//用fopen打开文件，然后循环读取，在放到函数handle_php_error_log中去操作
				$appserver = $k;
				$domainname = $serverDomain[$key];
				$fp = fopen(PHP_ERROR_LOG_DIR . $v, 'r');
					while ($message = fgets($fp)) {
						$error = php_error_log($message);
						//保存错误日志信息
						$error_log = new entity('php_error_log');
						$error_log->errorLevel   = $error['errorLevel'];
						$error_log->occurTime    = $error['occurTime'];
						$error_log->errorMessage = $error['errorMessage'];
						$error_log->errorFile    = $error['errorFile'];
						$error_log->errorLine    = $error['errorLine'];
						$error_log->chargeAuthor = $error['chargeAuthor'];
						$error_log->sourceFrom   = $key;
						$error_log->domainName   = $domainname;
						$error_log->appServer    = $k;
						$error_log->handleResult = 0;
						$G_PGLOB->DBCA->save($error_log,'error');
					}
				}
			}

		}
		
	}
}

?>